package com.mjk.manager.usercenter.scuser;


import com.mjk.common.base.error.BeeError;
import com.mjk.common.base.response.BeeResponse;
import com.mjk.common.base.work.BaseWork;
import com.mjk.common.base.work.annotation.BeeWork;
import com.mjk.common.core.jdbc.bean.BeePage;
import com.mjk.common.core.jdbc.service.idgenerator.IdGenerateType;
import com.mjk.common.core.jdbc.service.plus.idbservice.BeeDBService;
import com.mjk.common.core.jdbc.service.plus.wrap.BeeQueryWrapper;
import com.mjk.common.tools.encrypt.MD5Util;
import com.mjk.common.util.ObjectUtils;
import com.mjk.common.util.StrUtils;
import com.mjk.manager.amis.core.model.PlatUser;
import com.mjk.manager.amis.core.model.SysRole;
import com.mjk.manager.usercenter.scuser.dto.ScUserDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

@BeeWork(work = "scuser")
public class ScUserWorkProxy extends BaseWork {
		@Autowired
		private BeeDBService beeDBService;

		/**
		 * 获取scUser列表信息
         */
		public BeeResponse getScUserList(ScUserDto dto){
			BeeQueryWrapper wrapper = new BeeQueryWrapper(new PlatUser());
			if(ObjectUtils.isNotEmpty(dto.getUserName())){
				wrapper.condition("name like #{1} or login_name like #{1}","%"+dto.getUserName()+"%");
			}
			wrapper.setPage(dto.getPageStart(),dto.getPageSize());
			BeePage<PlatUser> users = beeDBService.findManyByWrapperWithPage(wrapper);

			List<String> roleIds = users.getList().stream()
					.filter(role -> !ObjectUtils.isEmpty(role.getRoles())) // 过滤掉空值
					.flatMap(item -> Arrays.stream(item.getRoles().split(",")))
					.toList();
			//获取用户角色信息
			if(!CollectionUtils.isEmpty(roleIds)) {
				wrapper = BeeQueryWrapper.getWrapper(SysRole.class);
				wrapper.in(SysRole::getId, roleIds);
				List<SysRole> roles = beeDBService.findManyByWrapper(wrapper);
				Map<String, String> roleMap = roles.stream()
						.collect(Collectors.toMap(SysRole::getId, SysRole::getDESCR));
				for (PlatUser user : users.getList()) {
					List<String> sb = new ArrayList<>();
					if (user.getRoles().contains(",")) {
						for (String role : user.getRoles().split(",")) {
							sb.add(roleMap.get(role));
						}
					} else {
						sb.add(roleMap.get(user.getRoles()));
					}
					user.setRoleName(StrUtils.join(sb, ","));
				}
			}
			return BeeError.BEE_SUC.tranceError(users);

		}
		/**
		 * 保存或修改scUser
         */
		public  BeeResponse saveOrUpdateScUser(PlatUser model){
			BeeQueryWrapper wrapper = new BeeQueryWrapper(model);
			model.setUPDATED_BY(model.getCreater());
			model.setUPDATED_AT(LocalDateTime.now());
			if(!CollectionUtils.isEmpty(model.getRoleIds())) {
				model.setRoles(StrUtils.join(model.getRoleIds(), ","));
			}
			if(ObjectUtils.isNotEmpty(model.getId())) {
				wrapper.eq(PlatUser::getId,model.getId());
				beeDBService.update(wrapper);
			}else {
				model.setId(IdGenerateType.AUTO_SNOWFLAKE.getGenerator().getId(model));
				model.setSALT(IdGenerateType.AUTO_UUID.getGenerator().getId(model));
				model.setDELETED(false);
				model.setSTATUS(1);
				model.setPASSWORD(MD5Util.lowMd5(model.getLOGIN_NAME()+model.getPASSWORD()+model.getSALT()));
				model.setCREATED_BY(model.getCreater());
				model.setCREATED_AT(LocalDateTime.now());
				beeDBService.save(model);
			}
			return BeeError.BEE_SUC.tranceError();
		}
		/**
		 * 删除scUser
         */
		public  BeeResponse delScUser(PlatUser model){
			needParams(model, "id");
			BeeQueryWrapper wrapper = new BeeQueryWrapper(model);
			wrapper.eq(PlatUser::getId,model.getId());
			beeDBService.delete(wrapper);
			return BeeError.BEE_SUC.tranceError();
		}
}